package com.sxpcwlkj.framework.config;


import com.sxpcwlkj.framework.config.properties.MailProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.email.jakarta.api.MailClient;
import org.dromara.email.jakarta.comm.config.MailSmtpConfig;
import org.dromara.email.jakarta.core.factory.MailFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * JavaMail 配置
 *
 * @author Michelle.Chung
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class MailConfig {

    private final MailProperties mailProperties;

    @Bean("sms4jMail")
    public MailClient sms4jMail(){
        //Sms4jSmsDaoConfig 执行操作监听处理
        MailSmtpConfig config = MailSmtpConfig.builder()
                .port(mailProperties.getPort().toString())
                .fromAddress(mailProperties.getFrom())
                .smtpServer(mailProperties.getHost())
                .username(mailProperties.getUser())
                .password(mailProperties.getPass())
                .nickName("品创网络")
                .isSSL(mailProperties.getSslEnable().toString())
                .isAuth(mailProperties.getStarttlsEnable().toString())
                .retryInterval(5)
                .maxRetries(1)
                .build();
        //这里的key可以是任何可对比类型，用于后续从工厂取出邮件实现类用
        MailFactory.put("sms4jMail",config);
        log.info("初始化 sms4jMail 配置已加载！");
        return  MailFactory.createMailClient("sms4jMail");
    }

}
